// RemoteListview.cpp : implementation file
//

#include "stdafx.h"
#include "MyFtp.h"
#include "RemoteListview.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRemoteListview

IMPLEMENT_DYNCREATE(CRemoteListview, CListView)

CRemoteListview::CRemoteListview()
{
}

CRemoteListview::~CRemoteListview()
{
}


BEGIN_MESSAGE_MAP(CRemoteListview, CListView)
	//{{AFX_MSG_MAP(CRemoteListview)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_COMMAND(IDM_NET_DEL, OnNetDel)
	ON_COMMAND(IDC_NEW_RDEC, OnNewRdec)
	ON_COMMAND(IDC_DEL_RDEC, OnDelRdec)
	ON_COMMAND(IDM_NET_RENAME, OnNetRename)
	ON_COMMAND(IDM_NET_LOADDEC, OnNetLoaddec)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteListview drawing

void CRemoteListview::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CRemoteListview diagnostics

#ifdef _DEBUG
void CRemoteListview::AssertValid() const
{
	CListView::AssertValid();
}

void CRemoteListview::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CRemoteListview message handlers
void CRemoteListview::OnInitialUpdate()
{
	CListView::OnInitialUpdate();
	
	CMainFrame* pFrame = (CMainFrame*)GetParentFrame();
	GetListCtrl().SetImageList(&pFrame->m_imageSmall,LVSIL_SMALL);

	SetWindowLong(m_hWnd,GWL_STYLE,WS_VISIBLE|WS_CHILD|WS_BORDER|LVS_REPORT|LVS_EX_FULLROWSELECT);
	CListCtrl* pList = &GetListCtrl();
	//col 1
	CString cols[]={_T("Filename"), _T("Filesize"), _T("Last Modified"), _T("Permission")};
	pList->InsertColumn(0,cols[0]);
	pList->SetColumnWidth(0,250);
	pList->SetTextColor(RGB(0,0,0));
	//col 2
	pList->InsertColumn(1,cols[1]);
	pList->SetColumnWidth(1,100);
	pList->SetTextColor(RGB(0,0,0));
	//clo 3
	pList->InsertColumn(2,cols[2]);
	pList->SetColumnWidth(2,100);
	pList->SetTextColor(RGB(0,0,0));
	//clo 4
		//clo 3
	pList->InsertColumn(3,cols[3]);
	pList->SetColumnWidth(3,100);
	pList->SetTextColor(RGB(0,0,0));
}


void CRemoteListview::InsertList(CString file, CString len, CString date,int n)
{
	//CMainFrame* pFrame = (CMainFrame *)GetParentFrame();
	if(n==0)
	{
		if(file==_T("."))
		{
			
		}
		else if(file==_T(".."))
		{
			int x;
			GetFileIcon(file,&x,FILE_ATTRIBUTE_DIRECTORY);
			int JustNowItem=GetListCtrl().InsertItem(0,file,x);
		}
		else
		{
			int x;
			GetFileIcon(file,&x,FILE_ATTRIBUTE_DIRECTORY);
			int JustNowItem=GetListCtrl().InsertItem(0,file,x);
			GetListCtrl().SetItemText(JustNowItem,1,len);
			GetListCtrl().SetItemText(JustNowItem,2,date);
			GetListCtrl().SetItemText(JustNowItem,3,_T("Folder"));
		}
	}
	else
	{
		int x;
		GetFileIcon(file,&x,FILE_ATTRIBUTE_NORMAL);
		int JustNowItem=GetListCtrl().InsertItem(0,file,x);
		GetListCtrl().SetItemText(JustNowItem,1,len);
		GetListCtrl().SetItemText(JustNowItem,2,date);
		GetListCtrl().SetItemText(JustNowItem,3,_T("File"));
	}
	GetListCtrl().UpdateData(FALSE);
}

void CRemoteListview::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int i=GetListCtrl().GetSelectionMark();
	CString temp=GetListCtrl().GetItemText(i,0);
	CMainFrame* pFrame = (CMainFrame* )GetParentFrame();
	if(temp == _T(""))
	{
		return;
	}

	if(temp == _T(".."))
	{
		pFrame->UpDir();

		return;
	}

	if(GetListCtrl().GetItemText(i,3)==_T("Folder"))
	{
		pFrame->EnterDir(temp);
	}
	else
	{
		pFrame->AddDownFtpFile(temp);
	}

	*pResult = 0;
}

void CRemoteListview::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	int i=GetListCtrl().GetSelectionMark();
	if(GetListCtrl().GetItemText(i,0)==_T(""))
	{
		CMenu menu;
		VERIFY(menu.LoadMenu(IDR_MENU1));    
		// popup View alternative
		DWORD pos=GetMessagePos();
		POINTS point;
		point=MAKEPOINTS(pos);
		CMenu* pPopup;
		pPopup=menu.GetSubMenu(2);
		pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
		
	}
	else
	{
		if(GetListCtrl().GetItemText(i,3)==_T("Folder"))
		{
			CMenu menu;
			VERIFY(menu.LoadMenu(IDR_MENU1));    
			// popup View alternative
			DWORD pos=GetMessagePos();
			POINTS point;
			point=MAKEPOINTS(pos);
			CMenu* pPopup;
			pPopup=menu.GetSubMenu(1);
			pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
		}
		else
		{
			CMenu menu;
			VERIFY(menu.LoadMenu(IDR_MENU1));    
			// popup View alternative
			DWORD pos=GetMessagePos();
			POINTS point;
			point=MAKEPOINTS(pos);
			CMenu* pPopup;
			pPopup=menu.GetSubMenu(0);
			pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
		}
	}
	
	*pResult = 0;
}

void CRemoteListview::OnNetDel() 
{
	// TODO: Add your command handler code here
	int i=GetListCtrl().GetSelectionMark();
	CString temp=GetListCtrl().GetItemText(i,0);
	CMainFrame* pFrame=(CMainFrame* )GetParentFrame();
	if(temp == _T(""))
	{
		return;
	}
	if(GetListCtrl().GetItemText(i,3)==_T("Folder"))
	{

	}
	else
	{
		//pFrame->FtpDel(temp);
	}
}

void CRemoteListview::OnNewRdec() 
{
	// TODO: Add your command handler code here
	//CMainFrame* pFrame=(CMainFrame* )GetParentFrame();
	//CDecDlg ddlg;
	//if(ddlg.DoModal()==IDOK)
	//{
	//	//pFrame->FtpMkd(ddlg.m_decName);
	//}
}

void CRemoteListview::OnDelRdec() 
{
	// TODO: Add your command handler code here
	int i=GetListCtrl().GetSelectionMark();
	CString temp=GetListCtrl().GetItemText(i,0);
	CMainFrame* pFrame=(CMainFrame* )GetParentFrame();
	if(temp==_T(""))
	{
		return;
	}
	if(GetListCtrl().GetItemText(i,3)==_T("Folder"))
	{
		//pFrame->FtpRmd(temp);
	}
	else
	{

	}
}

void CRemoteListview::OnNetRename() 
{
	// TODO: Add your command handler code here
	//CFileDlg fdlg;
	//if(fdlg.DoModal()==IDOK)
	//{
	//	CMainFrame* pFrame=(CMainFrame* )GetParentFrame();
	//	int i=GetListCtrl().GetSelectionMark();
	//	CString temp=GetListCtrl().GetItemText(i,0);
	//	pFrame->FtpReName(temp,fdlg.m_fileName);
	//}
}

void CRemoteListview::OnNetLoaddec() 
{
	// TODO: Add your command handler code here
	//int i=GetListCtrl().GetSelectionMark();
	//CString temp=GetListCtrl().GetItemText(i,0);
	//CMainFrame* pFrame=(CMainFrame* )GetParentFrame();
	//if(temp == _T(""))
	//{
	//	return;
	//}
	//else
	//{
	//	CString str;
	//	DWORD len=atoi(GetListCtrl().GetItemText(i,1));
	//	pFrame->m_local.GetWindowText(str);
	//	pFrame->AddWork(temp,TYPE_DOWN,STA_WAIT,len,str+temp,TYPE_DIR,POS_TAILER);
	//	if(pFrame->GetWorkCount()==1)
	//	{
	//		pFrame->DownFile();
	//	}
	//}
}

void CRemoteListview::GetFileIcon(CString &fileName, int *iIcon,DWORD Type)
{
	CString str=fileName;
	SHFILEINFO shFi;
	if(!SHGetFileInfo(str,Type,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_USEFILEATTRIBUTES)) 
	{ 
		return; 
	}
	*iIcon=shFi.iIcon;
	DestroyIcon(shFi.hIcon); 
	return ; 
}

void CRemoteListview::ClearList()
{
	GetListCtrl().DeleteAllItems();
}